import request from '@/utils/request'
import { removeToken, setToken } from '@/utils/storage'

// 获取验证码
export const setCode = (mobile) => {
  return async () => {
    await request({
      url: `/sms/codes/${mobile}`,
      method: 'get',
    })
  }
}
export const sendToken = (payload) => {
  return {
    type: 'login/token',
    payload,
  }
}

export const login = (data) => {
  return async (dispath) => {
    const res = await request({
      url: '/authorizations',
      method: 'post',
      data,
    })

    dispath(sendToken(res.data))
    setToken(res.data)
  }
}

// 退出登录/清除token
export const loginOut = () => {
  return (dispath) => {
    dispath({
      type: 'login/out',
    })
    removeToken()
  }
}
